package com.example.newfolderapp;

import android.app.Activity;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.material.textfield.TextInputLayout;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextInputLayout nameInputLayout = findViewById(R.id.username_wrapper);
        TextInputLayout passwordInputLayout = findViewById(R.id.password_wrapper);
        //开启错误提示
        passwordInputLayout.setErrorEnabled(true);
        //开启计数
        passwordInputLayout.setCounterEnabled(true);
        //设置最大长度
        passwordInputLayout.setCounterMaxLength(16);
        //加载自定义样式
        nameInputLayout.setHintTextAppearance(R.style.hintAppearence);

        nameInputLayout.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //文本变化前调用
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //文本变化时调用
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //文本变化后调用

            }
        });


    }

    public void submitLogin(View view) throws IOException{
        EditText userNameText = findViewById(R.id.username);
        EditText passwordText = findViewById(R.id.password);
        String userName = userNameText.getText().toString().trim();
        String password = passwordText.getText().toString().trim();
        String u = "http://43.139.139.78:9000/login";
        new Thread(new Runnable() {
            @Override
            public void run() {
                final ByteArrayOutputStream bos;
                try{
                    String data = "username="+userName+"&password="+password;
                    HttpURLConnection conn = (HttpURLConnection) new URL(u).openConnection();
                    conn.setReadTimeout(5000);
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setRequestProperty("Content_Type","application/json");
                    conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
                    OutputStream outputStream = conn.getOutputStream();
                    outputStream.write(data.getBytes());
                    if (conn.getResponseCode() == 200){
                        InputStream inputStream = conn.getInputStream();
                        //使用ByteArrayOutputStream读取或转换输入流为字符串
                        bos = new ByteArrayOutputStream();
                        int len = 0;
                        byte[] buffer = new byte[1024];
                        //将InputStream写入buffer
                        while ((len = inputStream.read(buffer))!= -1){
                            bos.write(buffer, 0 ,len);
                        }
                        //转String,然后转json
                        JSONObject json = JSONObject.parseObject(bos.toString("UTF-8"));
                        bos.flush();
                        inputStream.close();
                        bos.close();

                        if (json.get("res").equals(500)) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        //todo changeUI
                                    } catch (UnsupportedOperationException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });


//        Map<String,Object> params = new HashMap<String, Object>();
//        params.put("username",userName);
//        params.put("password",password);
//        Aquery aquery = new AQuery(getApplicationContext());


    }
}